package com.ug.system.controller;

import com.ug.common.constant.SecurityConstants;
import com.ug.common.response.ResponseMessage;
import com.ug.common.response.ResponseMessageFactory;
import com.ug.common.utils.SecurityUtil;
import com.ug.common.utils.StringUtil;
import com.ug.logs.annotation.Log;
import com.ug.system.entity.SysMenu;
import com.ug.system.entity.dto.SysMenuDto;
import com.ug.system.entity.vo.SysMenuTreeVO;
import com.ug.system.entity.vo.TreeSelectVO;
import com.ug.system.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author LY
 * @create 2024/7/17 10:00
 * @Description 菜单控制层
 */
@Tag(name = "SysMenuController", description = "菜单管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/menu")
public class SysMenuController {

    private final SysMenuService menuService;

    /**
     * 获取菜单树
     */
    @Operation(summary = "菜单树", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("获取菜单树")
    @GetMapping("/tree")
    public ResponseMessage<List<SysMenuTreeVO>> getMenuTree(){
        List<String> roles = SecurityUtil.getCurrentRoles();
        return ResponseMessageFactory.getSuccessResponseMessage(menuService.getMenuTree(roles));
    }

    /**
     * 获取权限列表
     */
    @Operation(summary = "权限列表", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log(value = "获取权限列表")
    @GetMapping("/permission")
    public ResponseMessage<List<String>> getPermission(){
        return ResponseMessageFactory.getSuccessResponseMessage(menuService.getPermission());
    }

    /**
     * 查询当前用户所有权限菜单
     */
    @Operation(summary = "菜单权限", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @GetMapping("/all")
    public ResponseMessage<List<SysMenu>> queryAllMenus(){
        List<String> roles = SecurityUtil.getCurrentRoles();
        return ResponseMessageFactory.getSuccessResponseMessage(menuService.queryAllMenus(roles));
    }

    /**
     * 获取菜单列表
     * @param sysMenuDto 请求入参dto
     */
    @Operation(summary = "菜单列表", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("获取菜单列表")
    @GetMapping("/table")
    public ResponseMessage<List<SysMenuTreeVO>> getMenuTable(SysMenuDto sysMenuDto){
        return ResponseMessageFactory.getSuccessResponseMessage(menuService.getMenuTable(sysMenuDto));
    }

    /**
     * 编辑菜单
     * @param sysMenu 请求菜单信息入参
     */
    @Operation(summary = "编辑菜单", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("编辑菜单")
    @PostMapping("/edit")
    public ResponseMessage<String> editMenu(@RequestBody SysMenu sysMenu){
        String str = StringUtil.getEditType(sysMenu.getMenuId());
        menuService.editMenu(sysMenu);
        return ResponseMessageFactory.getSuccessResponseMessage(str);
    }

    /**
     * 删除菜单
     * @param menuId 菜单id
     */
    @Operation(summary = "删除菜单", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log("删除菜单")
    @DeleteMapping("/del")
    public ResponseMessage<String> delMenu(@Parameter(description = "菜单menuId") @RequestParam("menuId") Long menuId){
        menuService.delMenu(menuId);
        return ResponseMessageFactory.getSuccessResponseMessage("删除成功");
    }

    /**
     * 获取菜单下拉树
     */
    @Operation(summary = "菜单下拉树", security = { @SecurityRequirement(name = SecurityConstants.TOKEN_HEADER) })
    @Log(value = "获取菜单下拉树")
    @GetMapping("/select")
    public ResponseMessage<List<TreeSelectVO>> getMenuTreeSelect(){
        return ResponseMessageFactory.getSuccessResponseMessage(menuService.getMenuTreeSelect());
    }
}
